#!/bin/sh

# ************************* 解析参数 *************************
usage="usage: $0 [-w workspace][-w] [-s code_sign_identity] [-h help]"
line_break_left="\n\033[32;1m*********"
line_break_right="***************\033[0m\n"

current_shell_path=$(dirname $0)
current_shell_path=${current_shell_path/\./$(pwd)}
export_path="${current_shell_path}/build"
workspace_name="HHCommonPod"

echo $*
echo $#
echo "*********************"
for arg in "$@"
do
    parameter=${arg%%=*}
    value=${arg#*=}
    case $parameter in
        -pod_library_name)
            pod_library_name=$value
        ;;
        -help)
            echo $usage
            exit 0
        ;;
        *)
            echo "${parameter} 非法变量"
            exit 1
        ;;
    esac
    echo $arg
    echo "变量：${parameter}"
    echo "值：${value}"
    echo '--------'
done

# 指定输出文件目录不存在则创建
if test -d "${export_path}"; then
    rm -rf ${export_path}
    echo "目录 $export_path 已存在"
fi
mkdir -pv ${export_path}
echo "${line_break_left} 保存归档文件和ipa的路径=${export_path} ${line_break_right}"

# 归档文件路径
export_archive_path="${export_path}/${scheme_name}.xcarchive"

echo "************************* 开始 打包 ********************************"

cd $current_shell_path/Example
pod install

echo "开始构建项目，项目信息如下：\n workspace: ${workspace_name}
    scheme:${scheme_name}
    configuration:Release
     archivePath:${export_archive_path}
    code_sign_identity=${code_sign_identity}"

# step 1. Clean
xcodebuild clean -workspace ${workspace_name}.xcworkspace \
-scheme ${pod_library_name} \
-configuration Release

echo " 没有指定code_sign_identity 和 provisioning_profile 开始使用 xcode 中默认配置打包......"

xcodebuild -workspace ${workspace_name}.xcworkspace \
 -scheme ${pod_library_name} \
 -configuration Release \
 -derivedDataPath "${export_path}/DerivedData" \
-sdk iphoneos

xcodebuild -workspace ${workspace_name}.xcworkspace \
 -scheme ${pod_library_name} \
 -configuration Release \
 -derivedDataPath "${export_path}/DerivedData" \
-sdk iphonesimulator

# 检查是否构建成功
pod_library_simulator="${export_path}/DerivedData/Build/Products/Release-iphonesimulator/${pod_library_name}/${pod_library_name}.framework/${pod_library_name}"
pod_library_iphoneos="${export_path}/DerivedData/Build/Products/Release-iphoneos/${pod_library_name}/${pod_library_name}.framework/${pod_library_name}"
# xcarchive 实际是一个文件夹不是一个文件所以使用 -d 判断
if test -f "${pod_library_simulator}"; then
    echo "${line_break_left} 模拟器版本构建成功 🚀 🚀 🚀 ${line_break_left}"
else
    echo "${line_break_left} 模拟器版本构建失败 😢 😢 😢 ${line_break_right}"
    exit 1
fi

if test -f "${pod_library_iphoneos}"; then
    echo "${line_break_left} iPhone版本构建成功 🚀 🚀 🚀 ${line_break_right}"
else
    echo "${line_break_left} iPhone版本构建失败 😢 😢 😢 ${line_break_right}"
    exit 1
fi

lipo -create "${pod_library_iphoneos}" "${pod_library_simulator}" -output "${pod_library_iphoneos}"

destPath="${current_shell_path}/HHCommonPod/Frameworks/${pod_library_name}"

# 指定输出文件目录不存在则创建
if test -d "${destPath}"; then
    rm -rf ${destPath}
    echo "目录 $destPath 已存在"
fi
cp -rf "${export_path}/DerivedData/Build/Products/Release-iphoneos/${pod_library_name}" "${current_shell_path}/HHCommonPod/Frameworks/"
 
echo "${line_break_left} ❤️ ❤️ ❤️ ${pod_library_name} ❤️ ❤️ ❤️ 构建成功 ${line_break_left}"
echo '\n'
echo '\n'
echo '\n'
echo '\n'
